import time

from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
from selenium.webdriver import ActionChains
from selenium.webdriver.support import expected_conditions

from utils import *


class TestBrowser:
    def setup(self):
        desired_caps = {
            'platformName': 'Android',
            'platformVersion': '6.0.1',
            'deviceName': 'emulator-5554',
            'resetKeyBoard': True,
            'unicodeKeyboard': True,
            'noReset': True,
            'automationName': 'Uiautomator2',
            "appPackage": "com.xueqiu.android",
            "appActivity": ".common.MainActivity",
        }

        self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
        self.driver.implicitly_wait(5)

    def teardown(self):
        # self.driver.quit()
        pass

    def test_browser(self):
        head_img = By.ID, 'cn.emoney.emstock:id/headIV'
        activity_btn = By.XPATH, '//*[@text="活动中心"]'
        get_element(self.driver, head_img).click()
        get_element(self.driver, activity_btn).click()
        print('------------------')
        print(self.driver.contexts)
        print('------------------')
        # time.sleep(10)
        # self.driver.switch_to.context('WEBVIEW_cn.emoney.emstock')
        time.sleep(2)
        self.driver.switch_to.context(self.driver.contexts[-1])
        print(self.driver.current_context)

        web_activity = (By.XPATH, '//*[@id="app"]/ul/li/img')
        WebDriverWait(self.driver, 20).until(expected_conditions.presence_of_element_located(web_activity))
        ActionChains(self.driver).click(get_element(self.driver, web_activity)).perform()
        # time.sleep(3)
        # self.driver.back()
        # time.sleep(2)
        # self.driver.back()
        # self.driver.switch_to.default_content()
        # time.sleep(3)
        #
        # get_element(self.driver, head_img).click()
        # get_element(self.driver, activity_btn).click()
        # self.driver.switch_to.window(self.driver.window_handles[-1])






